﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Repository;
using PersonalHealth.Data.Model;
using Core.UnitOfWork;

namespace PersonalHealth.Data.Repository
{
    public class TaskRepository : Repository<Task>, ITaskRepository
    {
        public TaskRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork)
        {
           
        }

        public IList<Task> GetUserTasks(int userid, int pageIndex, int pageCount, out int msgCount)
        {
            var q = DbSet.Include("User").Where(c => c.UserId == userid);

            msgCount = q.Count();

            return q.OrderByDescending(c => c.CreateDate).Skip((pageIndex - 1) * pageCount).Take(pageCount).ToList();
        }

        public IList<Task> GetUserTasks(int userid, TaskType type, int pageIndex, int pageCount, out int msgCount)
        {
            var q = DbSet.Include("User").Where(c => c.UserId == userid);

            if (type == TaskType.Goal)
            {
                q = q.Where(c => c.TaskTypeId == (int)TaskType.Goal);
                msgCount = q.Count();
                return q.OrderByDescending(c => c.CreateDate).Skip((pageIndex - 1) * pageCount).Take(pageCount).ToList();
            }

            if (type == TaskType.Schedule)
            {
                q = q.Where(c => c.TaskTypeId == (int)TaskType.Schedule);
                msgCount = q.Count();
                return q.OrderByDescending(c => c.CreateDate).Skip((pageIndex - 1) * pageCount).Take(pageCount).ToList();
            }

            throw new ArgumentException("error MessageType!");
        }
    }
}
